#include <stdio.h>
#include <stdlib.h>

#define INF 0x3f3f3f3f
#define MAXN 2005

typedef struct
{
    int x, y;
} Point;

Point queue[MAXN * MAXN];
int front, rear;

void initQueue() 
{
    front = rear = 0;
}

void enqueue(int x, int y) 
{
    queue[rear].x = x;
    queue[rear].y = y;
    rear++;
}

Point dequeue()
{
    return queue[front++];
}

int isEmpty()
{
    return front == rear;
}

int n, m, x0, y0;
char grid[MAXN][MAXN];
int solo_dist[MAXN][MAXN];
int dist1[MAXN][MAXN];
int dr[] = { -1, 1, 0, 0 };
int dc[] = { 0, 0, -1, 1 };

void precompute_solo_dist()
{
    initQueue();
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++) 
        {
            if (grid[i][j] == '@')
            {
                solo_dist[i][j] = 0;
                enqueue(i, j);
            }
            else 
            {
                solo_dist[i][j] = -1;
            }
        }
    }
    while (!isEmpty()) 
    {
        Point p = dequeue();
        int r = p.x, c = p.y;
        for (int d = 0; d < 4; d++)
        {
            int nr = r + dr[d], nc = c + dc[d];
            if (nr >= 0 && nr < n && nc >= 0 && nc < m) 
            {
                if (grid[nr][nc] != '#' && solo_dist[nr][nc] == -1) 
                {
                    solo_dist[nr][nc] = solo_dist[r][c] + 1;
                    enqueue(nr, nc);
                }
            }
        }
    }
}

int main()
{
    scanf_s("%d %d %d %d", &n, &m, &x0, &y0);
    x0--; y0--;
    for (int i = 0; i < n; i++) 
    {
        scanf_s("%s", grid[i]);
    }
    precompute_solo_dist();
    for (int i = 0; i < n; i++) 
    {
        for (int j = 0; j < m; j++) 
        {
            dist1[i][j] = -1;
        }
    }
    initQueue();
    dist1[x0][y0] = 0;
    enqueue(x0, y0);
    int ans = INF;
    while (!isEmpty()) 
    {
        Point p = dequeue();
        int r1 = p.x, c1 = p.y;
        int step = dist1[r1][c1];
        int r2 = 2 * x0 - r1, c2 = 2 * y0 - c1;
        for (int d = 0; d < 4; d++)
        {
            int nr1 = r1 + dr[d], nc1 = c1 + dc[d];
            int nr2 = r2 + dr[d ^ 1], nc2 = c2 + dc[d ^ 1];
            if (nr1 < 0 || nr1 >= n || nc1 < 0 || nc1 >= m || grid[nr1][nc1] == '#') continue;
            if (nr2 < 0 || nr2 >= n || nc2 < 0 || nc2 >= m || grid[nr2][nc2] == '#') continue;
            int exit1 = (grid[nr1][nc1] == '@') ? 1 : 0;
            int exit2 = (grid[nr2][nc2] == '@') ? 1 : 0;
            if (exit1 && exit2) 
            {
                if (step + 1 < ans) ans = step + 1;
            }
            else if (exit1) 
            {
                if (solo_dist[nr2][nc2] != -1) 
                {
                    int total = step + 1 + solo_dist[nr2][nc2];
                    if (total < ans) ans = total;
                }
            }
            else if (exit2) 
            {
                if (solo_dist[nr1][nc1] != -1) 
                {
                    int total = step + 1 + solo_dist[nr1][nc1];
                    if (total < ans) ans = total;
                }
            }
            else
            {
                if (dist1[nr1][nc1] == -1)
                {
                    dist1[nr1][nc1] = step + 1;
                    enqueue(nr1, nc1);
                }
            }
        }
    }
    if (ans == INF) printf("-1\n");
    else printf("%d\n", ans);
    return 0;
}